<?php
/**
 * GM相关操作
 * 
 * Created	: 2011-06-04
 * Modified : 2011-06-04
 * @author  : 陈立强
 */
class Gm
{
	
	/**
	 * 处理GM命令
	 * @param string $command
	 */
	public static function handleGmCmd($command)
	{
		global $DB, $L, $G;
		$command = substr($command, 1);
		
		$command = explode(' ', $command);
		$action = $command[0];
		
		$itemNameIdList = C('item/item_name_id_list');
		switch ($action) {
			case 'make':
				if (!isset($itemNameIdList[$command[1]])) {
					return false;
				}
				$itemId = $itemNameIdList[$command[1]];
				$num = (int)$command[2];
				if (empty($command[3])) {
					$quality = 0;
				} else {
					$quality = $G['quality_id'][$command[3]];
				}
				$return = self::makeItem($itemId, $num, $quality);
			break;
			
			default:
				;
			break;
		}
		return $return;
	}
	
	/**
	 * 生成一个物品
	 * @param int $itemId
	 * @param int $num
	 * @param int $quality
	 */
	public static function makeItem($itemId, $num, $quality=0)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/item_list');
		$cfgInfo = $cfgList[$itemId];
		$qualitySign = $G['id_quality'][$quality];
		
		// 生成金钱
		if ($itemId == 65535) {
			$sql = 'UPDATE user SET coin=coin+'.$num.' WHERE uid='.$_SESSION['uid'];
			$DB->exeSql($sql);
			return true;
		} elseif ($itemId < 1000 || $itemId == 51) {// 生成农产品,辅材
			$data = array(
				'title'		=> $cfgInfo['title'].$k,
				'item_id'	=> $itemId,
				'item_type' => $cfgInfo['type'],
				'num'		=> $num,
				'stack'		=> 1,
				'quality'	=> $quality,
				'lvl'		=> $cfgInfo['lvl']
			);
			$ret = Depot::in($uid, $data);
			return $ret['val'];
		} elseif ($itemId > 1000 && $itemId <= 2000) {// 生成种子
			$data = array(
				'title'		=> $cfgInfo['title'].$qualitySign,
				'item_id'	=> $itemId,
				'item_type' => $cfgInfo['type'],
				'num'		=> $v,
				'stack'		=> 1,
				'quality'	=> 0,
				'lvl'		=> $cfgInfo['lvl']
			);
			$ret = Depot::in($uid, $data);
			return $ret['val'];
		} else {
			return false;
		}
	}
	
}